import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import UserView from '../views/User.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      // redirect: 'user',
      children:[
        {
          path: '/user',
          name: 'user',
          component: UserView
        },
        {
          path: '/createartic',
          name: 'createartic',
          component: () => import('../views/Createartic.vue')
        },
        {
          path: '/articlist',
          name: 'articlist',
          component: () => import('../views/Articlist.vue')
        },
        {
          path: '/likesetting',
          name: 'likesetting',
          component: () => import('../views/Likesetting.vue')
        },
        {
          path: '/bannersetting',
          name: 'bannersetting',
          component: () => import('../views/Bannersetting.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    }
  ]
})

router.beforeEach((to, from, next)=>{
  if (to.path !== '/login') {
    const cookie = localStorage.getItem('cms_cookie')
    if (!cookie) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})
export default router
